import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import VueAxios from 'vue-axios'

Vue.use(Vuex)
Vue.use(VueAxios,axios)

const store=new Vuex.Store({
  state:{
    userlist:[],
    userinfo:""
  },
  mutations:{
    get_userlist(state,userlist){
      state.userlist=userlist
    },
    get_userinfo_by_id(state,userinfo){
      state.userinfo=userinfo
    },
    delete_userinfo_by_id(state,index){
      state.userlist.splice(index,1)
    },
    add_userinfo(state,userinfo){
      state.userlist.push(userinfo)
    }
  },
  actions:{
    getUserlist(context,userinfo){
      debugger;
      axios.post('http://localhost:2222/userinfo',userinfo).then(response=>{
        store.commit('get_userlist',response.data.list)
      })
    },
    getUserinfoById(context,userinfo){
      return new Promise((resolve, reject) => {
          axios.get('http://localhost:2222/userinfo/view/'+userinfo.id).then(response=>{
            store.commit('get_userinfo_by_id',response.data)
            resolve();
          })
      });
    },
    deleteUserinfoById(context,obj){
      store.commit('delete_userinfo_by_id',obj.index)
      // axios.get('http://localhost:2222/userinfo/delete/'+obj.userinfo.id).then(response=>{
      //   console.log(response.data);
      //   store.commit('delete_userinfo_by_id',obj.index)
      // })
    },
    addUserinfo(context,obj){
      store.commit('add_userinfo',obj);
      newuserinfo:{}
    }
  }
})

export default store
